<?php

class SJB_Miscellaneous_ContactForm extends SJB_Function
{
	public function execute()
	{
		$tp = SJB_System::getTemplateProcessor();
		$contact_form = SJB_ObjectMother::createContactForm();
		$contact_form->parseRequestedData($_REQUEST);
		$isCaptcha = 0;
		$captcha      = new SJB_Captcha($_REQUEST);
		$captcha_form = SJB_ObjectMother::createForm($captcha);
		if (SJB_PluginManager::isPluginActive('CaptchaPlugin') && SJB_System::getSettingByName('contactUsCaptcha') == 1) {
			$isCaptcha = 1;
			$contact_form->setCaptcha($captcha_form);
		}
		$captcha_form->registerTags($tp);
		$tp->assign('captcha', array_pop($captcha_form->form_fields));

		if ($contact_form->isFormSubmitted()) {

			if ($contact_form->isDataValid()) {
				$contact_form->sendMessage();
				$tp->assign('message_sent', true);
			} else {
				$tp->assign('field_errors', $contact_form->getFieldErrors());
			}
		}

		$contact_form->assignTemplateVariables($tp);
		$tp->assign('isCaptcha', $isCaptcha);
		$tp->display('contact_form.tpl');

	}
}

